package com.socket.netty;

import com.socket.batch.collector.DataAnalysis;
import com.socket.batch.collector.MessageCollector;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.ReferenceCountUtil;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by kaddddd on 2017/11/17 0017.
 */
public class ThreadDataHandler implements Runnable {
    private Socket client;

    public ThreadDataHandler(Socket i) {
        client=i;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = client.getInputStream();
            OutputStream outputStream = client.getOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            while((len = inputStream.read(buffer)) != -1){
                outputStream.write(buffer, 0, len);
            }
            outputStream.close();
            inputStream.close();
            byte[] req = output.toByteArray();

            String body = DataAnalysis.byte2hex(req);

            body += "5D";//添加分隔符
            //System.out.println("---------------接收到的消息为:"+body);

            //System.out.println("---------------接收到的消息长度为:"+DataAnalysis.hex2Bytes(body).length);

            body = body.replace("5A01", "5B");
            body = body.replace("5A02", "5A");
            body = body.replace("5E01", "5D");
            body = body.replace("5E02", "5E");
            String msgId = body.substring(18, 22);
            System.out.println("msgId:"+msgId);
            if(("1001").equals(msgId)){
                try {
                    body = body.replace(body.substring(138,144), "").substring(2);
                    byte[] bytes = DataAnalysis.hex2Bytes(body);
                    String s1 = "0000001F"+"00000000"+"100200BC614E0100010000000000"+"00"+"00000000";
                    String s = "5B" + s1 +Integer.toHexString(DateServerHandler.getCRC(DataAnalysis.hex2Bytes(s1))).toUpperCase()+"5D";
                    byte[] b = DataAnalysis.hex2Bytes(s);
                    outputStream.write(b);
                    outputStream.flush();
                    System.out.println("返回给客户端的数据是:"+DataAnalysis.byte2hex(s.getBytes()));

                } catch (Exception e){

                }
            }else if (("1005").equals(msgId)){
                try {
                    body = body.replace(body.substring(46,52), "").substring(2);
                    byte[] bytes = DataAnalysis.hex2Bytes(body);
                    //System.out.println("待校验的hex串:"+body);
                    String s1 = "0000001A00000000100600BC614E0100010000000000";
                    String s = "5B" + s1 +Integer.toHexString(DateServerHandler.getCRC(DataAnalysis.hex2Bytes(s1))).toUpperCase()+"5D";
                    //System.out.println("CRC Code:"+Integer.toHexString(getCRC(DataAnalysis.hex2Bytes(s1))).toUpperCase());
                    //System.out.println("返回给客户端的hex为:"+s);
                    byte[] b = DataAnalysis.hex2Bytes(s);
                    outputStream.write(b);
                    outputStream.flush();
                    System.out.println("返回给客户端的数据是:"+DataAnalysis.byte2hex(s.getBytes()));
                } catch (Exception e){

                }
            }
            else if (("1200").equals(msgId)){
                try {
                    String dataType = body.substring(90, 94);
                    System.out.println(dataType);
                    if(("1202").equals(dataType)){
                        MessageCollector.collect(DataAnalysis.hex2Bytes(body));
                    }
                    if(("1203").equals(dataType)){
                        MessageCollector.collect(DataAnalysis.hex2Bytes(body));
                    }
                } catch (Exception e){

                }

            }

            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

}
